﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Bussiness;

namespace ShoppingCartWEB
{
    public partial class ListProduct : System.Web.UI.Page
    {
        public IEnumerable<Product> list;
        private ProductManager man=new ProductManager();
        private ShoppingCard card =  new ShoppingCard();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                list = man.AllProduct(); 
                dlProduct.DataSource = list;
                dlProduct.DataBind();
            }
        }

        protected void dlProduct_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                int id = (int)dlProduct.DataKeys[e.Item.ItemIndex];
                Response.Write("xac nhan day la: "  + id);
                //add To Cart
                card.Cards = GetCart();
                card.AddProduct(man.GetProductById(id));
                Session["Cart"] = card.Cards;
                Response.Redirect("Cart.aspx");
            }
        }

        private Dictionary<Product, int> GetCart()
        {
            if (Session["Cart"] == null)
                Session.Add("Cart", new Dictionary<Product,int>());
            return (Dictionary<Product,int>)Session["Cart"];
        }

    }
}